/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.ac.model;

import java.net.URL;
import java.util.ResourceBundle;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.ac.view.AccessControlPanel;
import offset.nodes.client.dialog.newnode.model.AbstractNewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.NewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.ServiceProcessor;
import offset.nodes.client.dialog.newnode.model.Step;
import offset.nodes.client.dialog.newnode.model.ContentType;

/**
 *
 * @author Walter Lütgenau
 */
public class AccessControlNewNodeConfigurator extends AbstractNewNodeConfigurator implements NewNodeConfigurator {

    public ObjectType getObjectType() {
        ResourceBundle bundle = ResourceBundle.getBundle("offset.nodes.client.dialog.view.DialogBundle");

        ResourceBundle defaultBundle = ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle");
        ContentType[] folderContentTypes = new ContentType[]{
            new ContentType("", "none", "application/octet-stream", defaultBundle),
            new ContentType("binary", "binary", "application/octet-stream", defaultBundle),
            new ContentType("css", "css", "text/css", defaultBundle),
            new ContentType("html", "html", "text/html", defaultBundle)
        };
        ContentType[] fileContentTypes = new ContentType[]{
            new ContentType("", "none", "application/octet-stream", defaultBundle),
            new ContentType("binary", "binary", "application/octet-stream", defaultBundle),
            new ContentType("css", "css", "text/css", defaultBundle),
            new ContentType("html", "html", "text/html", defaultBundle),
            new ContentType("png", "png", "image/png", defaultBundle),
            new ContentType("gif", "gif", "image/gif", defaultBundle),
            new ContentType("tif", "tiff", "image/tiff", defaultBundle),
            new ContentType("jpg", "jpeg", "image/jpeg", defaultBundle)
        };

        AccessControlModel accessControlModel;
        try {
            accessControlModel = new AccessControlModel(new ServerModel(getService()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        AccessControlPanel accessControlPanel = new AccessControlPanel(accessControlModel, getPath());
        return new ObjectType(bundle, "accessControl.name", "accessControl.description", new Step[]{new Step(bundle, "accessControl.step1.name", accessControlPanel)}, new ServiceProcessor(getService(), getPath()) {

            @Override
            public URL process() {
                try {
                    AccessControlModel model = new AccessControlModel(new ServerModel(getService()));
                    AccessControlPanel panel = (AccessControlPanel) getObjectType().getSteps()[1].getPanel();

                    model.setNodeAccessControl(getPath(), panel.getPrincipals());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        });


    }
}
